
#include "time.h"

uint32_t _runtime=0;

void timer_init(void)
{
	_runtime = 0;
	//inits counter0 with interrupts occuring every 1ms
	OCR0 = 250;
	TIMSK |=(1<<OCIE0); //interrupts
	TCCR0 |= (0<<CS02) | (1<<CS01) | (0<<CS00); //scale
	TCCR0 |= (1<<WGM01) | (0<<WGM00) | (0<<COM01) | (0<<COM00);//waveform+port
	
}

ISR(TIMER0_COMP_vect)
{
	_runtime++;
}

/**
 * Returns MCU runtime in ms. Timer is reseted at startup, but 
 * can also be reseted by timer_init() function
 *
 * uint32_t overflow occurs after 4.2949e9ms => 1193 hours
 *
 * @return uint32_t runtime in milliseconds
 */
uint32_t time(void)
{
	return _runtime;
}
